<template>
  <div>
    <app-header />
    <div class="container page-container">
      <div class="page-header">
        <h1>创建文章</h1>
      </div>
      
      <el-card class="post-form-card">
        <post-form 
          :post="newPost" 
          :loading="loading"
          @submit="createPost"
        />
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import AppHeader from '../components/app-header.vue'
import PostForm from '../components/post-form.vue'

const router = useRouter()
const loading = ref(false)

const newPost = ref({
  title: '',
  category: '',
  summary: '',
  content: ''
})

const createPost = async (postData) => {
  loading.value = true
  
  try {
    await axios.post('/api/posts', {
      ...postData,
      author: JSON.parse(localStorage.getItem('user')).name,
      createdAt: new Date().toISOString(),
      comments: []
    })
    
    ElMessage.success('文章创建成功')
    router.push('/dashboard')
  } catch (error) {
    console.error('Failed to create post:', error)
    ElMessage.error('文章创建失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="less" scoped>
.page-header {
  margin-bottom: 20px;
  
  h1 {
    margin: 0;
    font-size: 28px;
    color: #303133;
  }
}

.post-form-card {
  margin-bottom: 30px;
}
</style>

